package com.lechuang.quanzai.view.activity.own;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lechuang.quanzai.R;
import com.lechuang.quanzai.model.LocalSession;
import com.lechuang.quanzai.model.bean.UpdataInfoBean;
import com.lechuang.quanzai.presenter.net.Netword;
import com.lechuang.quanzai.presenter.net.ResultBack;
import com.lechuang.quanzai.presenter.net.netApi.CommenApi;
import com.lechuang.quanzai.utils.PhotoUtil;
import com.lechuang.quanzai.utils.Utils;
import com.lechuang.quanzai.view.activity.ui.FindBackActivity;
import com.lechuang.quanzai.view.activity.ui.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.lechuang.quanzai.utils.PhotoUtil.t;

/**
 * 作者：li on 2017/11/10 20:28
 * 邮箱：961567115@qq.com
 * 修改备注:
 */
public class NewSertActivity extends AppCompatActivity implements View.OnClickListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private Context mContext = NewSertActivity.this;
    private TextView tv_code;
    private EditText et_change_phone, et_change_code,etmima;//手机号,验证码
    private LocalSession mSession;
    //保存用户登录信息的sp
    private SharedPreferences.Editor se;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password_number);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        se = PreferenceManager.getDefaultSharedPreferences(this).edit();

        findViewById(R.id.iv_back).setOnClickListener(this);
        tv_code = (TextView) findViewById(R.id.tv_code);
        //手机号
        et_change_phone = (EditText) findViewById(R.id.et_change_phone);
        et_change_phone = (EditText) findViewById(R.id.et_change_phone);
        etmima= (EditText)findViewById(R.id.etmima);
        //验证码
        ((TextView) findViewById(R.id.tv_title)).setText("修改密码");
        et_change_code = (EditText) findViewById(R.id.et_change_code);
        tv_code.setOnClickListener(this);
        findViewById(R.id.btn_change_ok).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_code:
                if (!Utils.isTelNumber(et_change_phone.getText().toString())) {
                    Utils.show(mContext, "请输入正确的手机号");
                    return;
                }
                changePwdSendCode(et_change_phone.getText().toString().trim());
                break;
            case R.id.btn_change_ok:
                if (!Utils.isTelNumber(et_change_phone.getText().toString())) {
                    Utils.show(mContext, "请输入正确的手机号");
                    return;
                }
                if (Utils.isEmpty(et_change_code)) {
                    Utils.show(mContext, "请输入验证码");
                    return;
                }
                if (Utils.isNetworkAvailable(mContext)) {
                    HashMap map = new HashMap();
                    map.put("phone", et_change_phone.getText().toString());
                    map.put("password", Utils.getMD5(etmima.getText().toString()));
                    map.put("verifiCode", et_change_code.getText().toString());
                    updatePwd(map);
                } else {
                    Utils.show(mContext, getString(R.string.net_error1));
                }
                break;
        }
    }

    /**
     * 修改密码获取验证码
     */
    private void changePwdSendCode(String phoneNumber) {
        Netword.getInstance().getApi(CommenApi.class)
                .updatePwdCode(phoneNumber)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<String>(this) {
                    @Override
                    public void successed(String result) {
                        Utils.show(NewSertActivity.this, result);
                        PhotoUtil.getCode(tv_code);
                        PhotoUtil.handler.post(t);

                    }
                });
    }

    /**
     * 修改密码
     *
     * @param map
     */
    private void updatePwd(HashMap map) {
        Netword.getInstance().getApi(CommenApi.class)
                .changePassword(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<String>(this) {
                    @Override
                    public void successed(String result) {
                        Utils.show(NewSertActivity.this, result);
                        SharedPreferences.Editor se = getSharedPreferences("login", MODE_PRIVATE).edit();
                        se.putString("login", et_change_phone.getText().toString());
                        se.commit();
                        startActivity(new Intent(NewSertActivity.this, LoginActivity.class));
                        finish();
                    }

                });
    }
}
